﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Security.Principal;
using System.Text.RegularExpressions;

namespace SIDTranslator
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class SIDTranslatorUI : Window
    {
        public bool hexFormat = true;

        public Regex hexSIDRegex = new Regex(@"^([a-fA-F0-9]{24})(([a-fA-F0-9]{2})*)$");
        public Regex stringSIDRegex = new Regex(@"^[sS]\-1\-([156789]|16)((-(\d)+)*)$");

        public SIDTranslatorUI()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            valueDst.Text = "";
            this.WindowStyle = WindowStyle.ThreeDBorderWindow;
            alwaysOnTop.IsChecked = false;
            this.Topmost = false;
            copy.Content = "Copy";
            translate.Content = "Translate";
        }

        private void Translate_Click(object sender, RoutedEventArgs e)
        {
            string sid = valueSrc.Text.Trim().Replace(" ", "").Replace("0x", "");
            errorTxt.Text = "";
            copy.IsEnabled = true;
            if (stringSIDRegex.IsMatch(sid))
            {
                try
                {
                    valueDst.Text = stringToHex(sid);
                }
                catch (Exception ex)
                {
                    errorTxt.Text = "Invalid SID format";
                    valueDst.Text = "";
                    copy.IsEnabled = false;
                }
            }
            else if (hexSIDRegex.IsMatch(sid))
            {
                try
                {
                    valueDst.Text = hexToString(sid);
                }
                catch (Exception ex)
                {
                    errorTxt.Text = "Invalid SID format";
                    valueDst.Text = "";
                    copy.IsEnabled = false;
                }
            }
            else
            {
                errorTxt.Text = "Invalid SID format";
            }
        }

        private void Compare_Click(object sender, RoutedEventArgs e)
        {
            string sidSrc = valueSrc.Text.Trim().Replace(" ", "").Replace("0x", "");
            string sidDst = valueDst.Text.Trim().Replace(" ", "").Replace("0x", "");
            string sid01 = "sid01";
            string sid02 = "sid02";
            errorTxt.Text = "";

            if (!stringSIDRegex.IsMatch(sidSrc) && !hexSIDRegex.IsMatch(sidSrc))
            {
                errorTxt.Text = "First SID has an invalid format";
            }
            else if (!stringSIDRegex.IsMatch(sidDst) && !hexSIDRegex.IsMatch(sidDst))
            {
                errorTxt.Text = "Second SID has an invalid format";
            } else 
            {
                try 
	            {
                    if (stringSIDRegex.IsMatch(sidSrc) && stringSIDRegex.IsMatch(sidDst)) { sid01 = stringToHex(sidSrc); sid02 = stringToHex(sidDst); }
                    if (hexSIDRegex.IsMatch(sidSrc) && hexSIDRegex.IsMatch(sidDst)) { sid01 = stringToHex(hexToString(sidSrc)); sid02 = stringToHex(hexToString(sidDst)); }
                    if (hexSIDRegex.IsMatch(sidSrc) && stringSIDRegex.IsMatch(sidDst)) { sid01 = stringToHex(hexToString(sidSrc)); sid02 = stringToHex(sidDst); }
                    if (stringSIDRegex.IsMatch(sidSrc) && hexSIDRegex.IsMatch(sidDst)) { sid01 = stringToHex(sidSrc); sid02 = stringToHex(hexToString(sidDst)); }
                    if (sid01.Replace(" ", "") == sid02.Replace(" ", ""))
                    { errorTxt.Text = "Result : SID are equal"; }
                    else
                    { errorTxt.Text = "Result : SID are not equal"; }
	            }
	            catch (Exception)
	            {
                    errorTxt.Text = "One SID has an invalid format";
	            }
            }
        }

        private void paste_Click(object sender, RoutedEventArgs e)
        {
            valueSrc.Text = System.Windows.Clipboard.GetText();
        }

        private void pasteDst_Click(object sender, RoutedEventArgs e)
        {
            valueDst.Text = System.Windows.Clipboard.GetText();
        }

        private void copy_Click(object sender, RoutedEventArgs e)
        {
            Clipboard.SetText(valueDst.Text);
        }

        private void alwaysOnTop_Checked(object sender, RoutedEventArgs e)
        {
            this.Topmost = true;
        }

        private void alwaysOnTop_Unchecked(object sender, RoutedEventArgs e)
        {
            this.Topmost = false;
        }

        private void about_Click(object sender, RoutedEventArgs e)
        {
            var newWindow = new About();
            newWindow.Show();
        }

        private void rTranslate_Checked(object sender, RoutedEventArgs e)
        {
            copy.Content = "Copy";
            translate.Content = "Translate";
            copy.Click -= pasteDst_Click;
            copy.Click += copy_Click;
            valueDst.IsEnabled = false;
            valueDst.IsReadOnly = true;
            translate.Click -= Compare_Click;
            translate.Click += Translate_Click;
            
        }

        private void rCompare_Checked(object sender, RoutedEventArgs e)
        {
            copy.Content = "Paste";
            copy.IsEnabled = true;
            translate.Content = "Compare";
            copy.Click -= copy_Click;
            copy.Click += pasteDst_Click;
            valueDst.IsEnabled = true;
            valueDst.IsReadOnly = false;
            translate.Click += Compare_Click;
            translate.Click -= Translate_Click;
        }

        public string stringToHex(string SID)
        {
            SecurityIdentifier sec = new SecurityIdentifier(SID);
            byte[] bSID = new byte[sec.BinaryLength];
            sec.GetBinaryForm(bSID, 0);
            string dSID = string.Empty;
            foreach (byte bTemp in bSID)
            {
                dSID += bTemp.ToString("X2") + " ";
            }
            return dSID.TrimEnd();
        }

        public string hexToString(string SID)
        {
            int sidLength = SID.Length;
            byte[] bytes = new byte[sidLength / 2];
            for (int i = 0; i < sidLength; i += 2)
            {
                bytes[i / 2] = Convert.ToByte(SID.Substring(i, 2), 16);
            }
            SecurityIdentifier sSID = new SecurityIdentifier(bytes, 0);
            return sSID.ToString();
        }
    }
}
